# -*- coding:UTF-8 -*-#

'''
Created on 2012-4-7

@author: qianwp
'''
from popcan.core.structures import Singleton
from popcan.cache.cachedaos import CustomerCacheDao,DesignerCacheDao,AlbumCacheDao,AlbumIndexCacheDao,SessionCacheDao
from popcan.db.daos import CustomerDao,DesignerDao,AlbumDao
from popcan.model.models import AlbumIndex,Album,AlbumDesc,Customer,Session
from popcan.redis.redisdaos import AlbumDescRedisDao
from popcan.helper.uidhelper import AlbumUIDCreator,CustUIDCreator
from popcan.helper.requesthelper import createSessionId
import time

class CustomerAccessor(Singleton):
    
    def getCustomer(self,cust_id):
        customer = CustomerCacheDao().get(cust_id)
        if not customer:
            customer = CustomerDao().getCustomerById(cust_id)
        return customer
    
    def getCachedCustomer(self,cust_id):
        return CustomerCacheDao().get(cust_id)
    
    def createCustomer(self,email,nick,passwd):
        cust_id = CustUIDCreator().nextuid()
        logindate = regdate = time.time()
        customer = Customer(cust_id,email,passwd,nick,regdate,logindate)
        CustomerDao().addCustomer(customer)
        CustomerCacheDao().save(customer)
        return customer
    
    def getCustomerByEmail(self,email):
        return CustomerDao().getCustomerByEmail(email)
    
    def updateLoginDate(self,customer):
        customer.logindate = time.time()
        CustomerDao().updateLoginDate(customer)
        CustomerCacheDao().save(customer)
        return customer
    
    def updatePasswd(self,customer,passwd):
        customer.passwd = passwd
        CustomerDao().updatePasswd(customer)
        CustomerCacheDao().save(customer)
        return customer
    
class SessionAccessor(Singleton):
    
    def createSession(self,cust_id,remoteip):
        sessionId = createSessionId()
        session = Session(sessionId,cust_id,remoteip)
        SessionCacheDao().save(session) 
        return session
    
    def removeSession(self,session_id):
        SessionCacheDao().delete(session_id)
        
    def getSession(self,session_id):
        return SessionCacheDao().get(session_id)
    
class DesignerAccessor(Singleton):
    
    def getDesigner(self,cust_id):
        designer = DesignerCacheDao().get(cust_id)
        if not designer:
            designer = DesignerDao().getDesignerById(cust_id)
        return designer
    
class AlbumAccessor(Singleton):
    
    def getAlbum(self,album_id):
        album = AlbumCacheDao().get(album_id)
        if not album:
            album = AlbumDao().getAlbumById(album_id)
        return album
    
    def getAlbumIndexs(self,cust_id):
        albumIndexs = AlbumIndexCacheDao().get(cust_id)
        if not albumIndexs:
            albumIds = AlbumDao().getAlbumIdsByCustId(cust_id)
            albumIndexs = AlbumIndex(cust_id)
            for albumId in albumIds:
                albumIndexs.add(albumId)
        return albumIndexs
    
    def getAlbumList(self,cust_id):
        albumIds = self.getAlbumIndexs(cust_id)
        albums = []
        for albumId in albumIds.getIndexIds():
            album = self.getAlbum(albumId)
            albums.append(album)
        return albums
    
    def getDesc(self,album_id):
        albumDesc = AlbumDescRedisDao().get(album_id)
        if albumDesc:
            return albumDesc.desc
        return None
    
    def createAlbum(self,cust_id,name,desc):
        albumIndexs = self.getAlbumIndexs(cust_id)
        albumId = AlbumUIDCreator().nextuid()
        album = Album(albumId,cust_id,name,time.time())
        albumIndexs.add(albumId)
        albumdesc = AlbumDesc(albumId,desc)
        AlbumDao().addAlbum(album)
        AlbumCacheDao().save(album)
        AlbumIndexCacheDao().save(albumIndexs)
        AlbumDescRedisDao().save(albumdesc)
        return album
    
    def removeAlbum(self,cust_id,album_id):
        albumIndexs = self.getAlbumIndexs(cust_id)
        if not albumIndexs.exists(album_id):
            return False
        albumIndexs.remove(album_id)
        AlbumIndexCacheDao().save(albumIndexs)
        AlbumCacheDao().delete(album_id)
        AlbumDao().removeAlbumById(album_id)
        AlbumDescRedisDao().delete(album_id)
        return True
    
    def modifyDesc(self,cust_id,album_id,desc):
        album = self.getAlbum(album_id)
        if not album or album.custId <> cust_id:
            return False
        albumdesc = AlbumDesc(album_id,desc)
        AlbumDescRedisDao().save(albumdesc)
        return True
    
    def modifyName(self,cust_id,album_id,name):
        album = self.getAlbum(album_id)
        if not album or album.custId <> cust_id:
            return False
        album.name = name
        AlbumCacheDao().save(album)
        AlbumDao().updateAlbumName(album)
        return True
    
class CompositAccessor(Singleton):
    
    pass

class PhotoAccessor(Singleton):
    
    pass

class CommentAccessor(Singleton):
    
    pass